package com.tss.utils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.UUID;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class CodeUtil {
	public static String getUUID(){
		String s = UUID.randomUUID().toString();
		s = s.replaceAll("-", "");
		return s;
	}
	
	/**
	 * AES加密
	 * @param content
	 * @param password
	 * @return
	 */
	public static String AESEcode(String content,String password){
		try {             
            KeyGenerator kgen = KeyGenerator.getInstance("AES");  
            kgen.init(128, new SecureRandom(password.getBytes()));  
            SecretKey secretKey = kgen.generateKey();  
            byte[] enCodeFormat = secretKey.getEncoded();  
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");  
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器  
            byte[] byteContent = content.getBytes("utf-8");  
            cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化  
            byte[] result = cipher.doFinal(byteContent);  
            return new String(result,"utf-8"); // 加密  
    } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
    } catch (NoSuchPaddingException e) {  
            e.printStackTrace();  
    } catch (InvalidKeyException e) {  
            e.printStackTrace();  
    } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
    } catch (IllegalBlockSizeException e) {  
            e.printStackTrace();  
    } catch (BadPaddingException e) {  
            e.printStackTrace();  
    }  
    return null;  
	}
	
	public static String AESDecode(String content,String password){
		try {  
            KeyGenerator kgen = KeyGenerator.getInstance("AES");  
            kgen.init(128, new SecureRandom(password.getBytes()));  
            SecretKey secretKey = kgen.generateKey();  
            byte[] enCodeFormat = secretKey.getEncoded();  
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");              
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器  
           cipher.init(Cipher.DECRYPT_MODE, key);// 初始化  
           byte[] result = cipher.doFinal(content.getBytes("utf-8"));  
           return new String(result); // 加密  
   } catch (NoSuchAlgorithmException e) {  
           e.printStackTrace();  
   } catch (NoSuchPaddingException e) {  
           e.printStackTrace();  
   } catch (InvalidKeyException e) {  
           e.printStackTrace();  
   } catch (IllegalBlockSizeException e) {  
           e.printStackTrace();  
   } catch (BadPaddingException e) {  
           e.printStackTrace();  
   } catch (UnsupportedEncodingException e) {
	e.printStackTrace();
}  
   return null;  
	}
}
